home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue24 / survive / FMALLO.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-06-23  |  5.6 KB  |  207 lines

  1. unit fmAllo;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   Grids, StdCtrls, uBase, dmData;
  8.  
  9. type
  10.   TfrmPaymentAllocation = class(TForm)
  11.     grpSummary: TGroupBox;
  12.     grpCredits: TGroupBox;
  13.     btnPost: TButton;
  14.     btnCancel: TButton;
  15.     btnDefault: TButton;
  16.     grdSummary: TStringGrid;
  17.     grdCredits: TStringGrid;
  18.     procedure grdSummaryTopLeftChanged(Sender: TObject);
  19.     procedure grdCreditsTopLeftChanged(Sender: TObject);
  20.     procedure grdCreditsSetEditText(Sender: TObject; ACol, ARow: Longint;
  21.       const Value: string);
  22.     procedure FormCreate(Sender: TObject);
  23.     procedure FormShow(Sender: TObject);
  24.     procedure btnCancelClick(Sender: TObject);
  25.     procedure FormDestroy(Sender: TObject);
  26.   private
  27.   public
  28.     Credits: TList;
  29.     procedure AddCredit(aCreditNo, aAmount: LongInt);
  30.     procedure DeleteCredit(aCreditNo: LongInt);
  31.     procedure PopulateForm;
  32.   end;
  33.  
  34. var
  35.   frmPaymentAllocation: TfrmPaymentAllocation;
  36.  
  37. function ShowPaymentAllocationDlg: TModalResult;
  38.  
  39. implementation
  40.  
  41. uses fmpymt;
  42.  
  43. {$R *.DFM}
  44.  
  45. { TfrmPaymentAllocation }
  46.  
  47. function ShowPaymentAllocationDlg: TModalResult;
  48. begin
  49.   with frmPaymentAllocation do begin
  50.     PopulateForm;
  51.     if grdCredits.RowCount = 1 then
  52.       raise Exception.Create('No credits have been selected for payment');
  53.     Result := ShowModal;
  54.   end;
  55. end;
  56.  
  57. procedure TfrmPaymentAllocation.AddCredit(aCreditNo, aAmount: LongInt);
  58. begin
  59.   with grdCredits do begin
  60.     Credits.Add(Pointer(aCreditNo));
  61.     RowCount := RowCount + 1;
  62.     FixedRows := 1;
  63.     Cells[0, RowCount - 1] := Format(mskCurrency, [aAmount * 1.0]); { Amount }
  64.     Cells[1, RowCount - 1] := Format(mskCurrency, [aAmount * 1.0]); { Amount remaining }
  65.   end;
  66. end;
  67.  
  68. procedure TfrmPaymentAllocation.DeleteCredit(aCreditNo: LongInt);
  69. begin
  70.   Credits.Remove(Pointer(aCreditNo));
  71. end;
  72.  
  73. procedure TfrmPaymentAllocation.PopulateForm;
  74. var
  75.   I: Integer;
  76.   Total: LongInt;
  77.   PaymentAmount: LongInt;
  78. begin
  79.   with grdSummary do begin
  80.     Total := 0;
  81.     for I := FixedCols to ColCount do begin
  82.       with frmPayment.grdPayment do
  83.         PaymentAmount := GetCellAmount(Cells[1, I - 1]);
  84.       Inc(Total, PaymentAmount);
  85.       Cells[I, 1] := Format(mskCurrency, [PaymentAmount * 1.0]);
  86.       Cells[I, 2] := Format(mskCurrency, [PaymentAmount * 1.0]);
  87.     end;
  88.  
  89.     { update total payment and total remaining }
  90.     Cells[1, 1] := Format(mskCurrency, [Total * 1.0]);
  91.     Cells[1, 2] := Format(mskCurrency, [Total * 1.0]);
  92.   end;
  93. end;
  94.  
  95. procedure TfrmPaymentAllocation.grdSummaryTopLeftChanged(Sender: TObject);
  96. begin
  97.   { keeps the two grids synchronized when scrolling }
  98.   grdCredits.LeftCol := grdSummary.LeftCol;
  99. end;
  100.  
  101. procedure TfrmPaymentAllocation.grdCreditsTopLeftChanged(Sender: TObject);
  102. begin
  103.   { keeps the two grids synchronized when scrolling }
  104.   grdSummary.LeftCol := grdCredits.LeftCol;
  105. end;
  106.  
  107. procedure TfrmPaymentAllocation.grdCreditsSetEditText(Sender: TObject;
  108.   ACol, ARow: Longint; const Value: string);
  109. var
  110.   Total: LongInt;
  111.   I: Integer;
  112.   TotalRemaining: LongInt;
  113.   P: LongInt;
  114. begin
  115.   with grdCredits do
  116.   begin
  117.     if (ACol >= FixedCols) and (ARow >= FixedRows) then begin
  118.  
  119.       with grdSummary do begin
  120.         { get current total payment remaining, subtract out the previous
  121.           total for the affected payment method }
  122.         TotalRemaining := GetCellAmount(Cells[1, 2]) - GetCellAmount(Cells[ACol, 2]);
  123.  
  124.         { Recompute the amount remaining for the payment method }
  125.         Total := GetCellAmount(Cells[ACol, FixedRows]);
  126.         with grdCredits do
  127.           for I := FixedRows to RowCount - 1 do begin
  128.             P := GetCellAmount(Cells[ACol, I]);
  129.             Dec(Total, P);
  130.           end;
  131.         Inc(TotalRemaining, Total);
  132.         Cells[ACol, FixedRows + 1] := Format(mskCurrency, [Total * 1.0]);
  133.  
  134.         { display new total remaining }
  135.         Cells[1, 2] := Format(mskCurrency, [TotalRemaining * 1.0]);
  136.       end;
  137.  
  138.       { Recompute the amount remaining for the credit }
  139.       with grdCredits do begin
  140.         Total := GetCellAmount(Cells[0, ARow]);
  141.         for I := FixedCols to ColCount - 1 do
  142.           Dec(Total, GetCellAmount(Cells[I, ARow]));
  143.         Cells[1, ARow] := Format(mskCurrency, [Total * 1.0]);
  144.       end;
  145.     end;
  146.   end;
  147. end;
  148.  
  149. procedure TfrmPaymentAllocation.FormCreate(Sender: TObject);
  150. var
  151.   I: Integer;
  152. begin
  153.   Credits := TList.Create;
  154.  
  155.   { Display the headers for the summary grid }
  156.   with grdSummary do begin
  157.     Cells[0, 1] := 'Payment Amount';
  158.     Cells[0, 2] := 'Amount Remaining';
  159.     Cells[1, 0] := 'Total';
  160.  
  161.     with dmDataModule.PaymentMethodsList do begin
  162.       ColCount := Count + 2;
  163.       for I := 0 to Count - 1 do begin
  164.         Cells[I + 2, 0] := Strings[I];
  165.         grdCredits.Cells[I + 2, 0] := Strings[I];
  166.       end;
  167.     end;
  168.   end;
  169.  
  170.   { Display the headers for the allocation grid }
  171.   with grdCredits do begin
  172.     ColCount := grdSummary.ColCount;
  173.     RowCount := 1;
  174.     Cells[0, 0] := 'Amount';
  175.     Cells[1, 0] := 'Remaining';
  176.   end;
  177. end;
  178.  
  179. procedure TfrmPaymentAllocation.FormDestroy(Sender: TObject);
  180. begin
  181.   Credits.Free;
  182. end;
  183.  
  184. procedure TfrmPaymentAllocation.FormShow(Sender: TObject);
  185. begin
  186.   with grdCredits do begin
  187.     Col := FixedCols;
  188.     Row := FixedRows;
  189.     SetFocus;
  190.   end;
  191. end;
  192.  
  193. procedure TfrmPaymentAllocation.btnCancelClick(Sender: TObject);
  194. var
  195.   C, R: Integer;
  196. begin
  197.   with grdCredits do begin
  198.     for C := FixedCols to ColCount - 1 do begin
  199.       grdSummary.Cells[C, FixedRows + 1] := '';
  200.       for R := FixedRows to RowCount - 1 do
  201.         Cells[C, R] := '';
  202.     end;
  203.   end;
  204. end;
  205.  
  206. end.
  207.